<!-- 车辆基本信息 -->
<!DOCTYPE>
<html>

<head>
    <%- include ../taglib/layout-table.ejs %>

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content" style="padding:5px">
    <form method="post" enctype="multipart/form-data" action="/baseCar/file-upload">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="file" name="thumbnail">
        <input type="submit">
    </form>
</div>

<%- include ../taglib/layout-content.ejs %>

<script src="js/combox/comb-carType-data.js"></script>
<script src="js/combox/comb-terminal-data.js"></script>

<script type="text/javascript">
    var table;
    var validator,validatorfast;
    $(document).ready(function () {
        table = $('.dataTables-example').dataTable({
            "lengthChange": false,//是否允许用户自定义显示数量
//            "searching": false,//本地搜索
            "scrollY": true,//dt高度
            scrollX: true,
            "autoWidth": true,//自动宽度
            "dom": '<"top"<"clear">>rt<"bottom"ilp<"clear">>',
//            "lengthChange": false,//是否允许用户自定义显示数量
            "ajax": {
                "url": "/baseCar/list",
                "dataSrc": "rows"
            },
            serverSide: true,
            columns: [
                {
                    "sClass": "text-center",
                    "data": "id",
                    'checkboxes': {
                        'selectRow': true,
                    }
                },
                {"sClass": "text-center", data: 'id'},
                {"sClass": "text-center", data: 'deptId'},
                {"sClass": "text-center", data: 'baseCarNo', "bSortable": false, "orderable": false},
//                {"sClass": "text-center", data: 'baseCarNoColour', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarBrand', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarTypeId', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarOutput', "bSortable": false, "orderable": false},
//                {"sClass": "text-center", data: 'baseCarIconId', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseTerminalId', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseTerminalSim', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'userName', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'userPhone', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarName', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarFdNo', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarJjNo', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarRlType', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarOfProvince', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarOfCity', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarDis', "bSortable": false, "orderable": false},

//              add --- begin
                {"sClass": "text-center", data: 'baseBarColor', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarServiceNo', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarSeat', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarLoad', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarFactory', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarBuy', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'baseCarDisMake', "bSortable": false, "orderable": false},




//              add --- end




                {
                    "bSortable": false,//禁止排序
                    "orderable": false,
                    "sClass": "text-center",
                    data: function (d, e, f, g) {//这里给最后一列返回一个操作列表
                        //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                        var id = d.id
                        var index = g.row
                        return '' +
                                '<button class="btn btn-info btn-sm btn-select-row" type="button" onclick="edit(' + index + ')" data-toggle="modal"   ><i class="fa fa-edit"></i> <span class="bold">编辑</span>' +
                                '<button class="btn btn-danger btn-sm btn-select-row" type="button"  onclick="del(' + id + ')" ><i class="fa fa-close"></i> <span class="bold">删除</span>';

                    }
                }
            ],
            //排序
            'order': [[1, 'desc']],
            initComplete: function (setting, json) {
                //隐藏列
                debugger;
                visiblecolumn(1, false);
                visiblecolumn(2, false);
            },
        });

        //验证
        var cusrules = {
            rules: {
                memo: {
                    required: true,
                    //minlength: 3,
                    //maxlength: 20
                }, baseCarNo: {
                    required: true,
                    //minlength: 3,
                    //maxlength: 20
                }, baseTerminalId: {
                    required: true,
                    //minlength: 3,
                    //maxlength: 20
                }, baseTerminalSim: {
                    required: true,
                    //minlength: 3,
                    //maxlength: 20
                },
            },
            messages: {
                memo: {
                    required: icon + "请输入",
                    //minlength: icon + "必须3个字符以上",
                    //maxlength: icon + "必须20个字符以下"
                }, baseCarNo: {
                    required: icon + "请输入车牌号",
                    //minlength: icon + "车牌号必须3个字符以上",
                    //maxlength: icon + "车牌号必须20个字符以下"
                }, baseTerminalId: {
                    required: icon + "请输入设备id",
                    //minlength: icon + "设备id必须3个字符以上",
                    //maxlength: icon + "设备id必须20个字符以下"
                }, baseTerminalSim: {
                    required: icon + "请输入设备sim卡号",
                    //minlength: icon + "设备sim卡号必须3个字符以上",
                    //maxlength: icon + "设备sim卡号必须20个字符以下"
                },
            }

        };
        validator = $("#editForm").validate(cusrules);
        validatorfast = $("#editFormFast").validate(cusrules);
        loadComb("baseTerminalIdsearch", combTerminal);

//        下拉列表
        $("#myModal").on("shown.bs.modal", function () {
            loadComb("baseTerminalId", combTerminal);
            loadComb("baseCarTypeId", combCarType);

        });
        $("#myModalSimpal").on("shown.bs.modal", function () {
            loadComb("baseTerminalId_fast", combTerminal);

        });

        var start = {
            elem: '#baseCarFactory',
            format: 'YYYY-MM-DD hh:mm:ss',
            max: '2999-06-16 23:59:59', //最大日期

            istime: true,
            istoday: false,
            festival: true, //是否显示节日
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }

        };
        var end = {
            elem: '#baseCarBuy',
            format: 'YYYY-MM-DD hh:mm:ss',
            max: '2999-06-16 23:59:59',
            istime: true,
            istoday: false,
            festival: true, //是否显示节日
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
        $("#start").val(laydate.now() + ' 00:00:00');
        $("#end").val(laydate.now() + ' 23:59:59');




    });
    //数字框
    (function ($) {
        $('.spinner .btn:first-of-type').on('click', function () {
            debugger;
            var num = parseFloat($('.spinner input').val());
            if (num < 9) {
                $('.spinner input').val((parseFloat($('.spinner input').val()) + 0.1).toFixed(1));
            }

        });
        $('.spinner .btn:last-of-type').on('click', function () {
            var num = parseFloat($('.spinner input').val());
//            if(!num){
//                num=0;
//            }
            if (num > 1) {
                $('.spinner input').val((parseFloat($('.spinner input').val()) - 0.1).toFixed(1));
            }


        });
    })(jQuery);




    function add() {
        //选择tree
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        debugger;
        if (selectedNode.isParent) {
            swal("选择错误", "此区域无法添加车辆!", "warning");
            return false;
        } else {

        }
        $.ajax({
            type: "GET",
            url: "/baseCar/create",
            cache: false,
            async: false,
            dataType: "json",
            success: function (data) {
                $('#editForm').loadDate(data);
                $("#myModal").modal("show");
            }
        });

    }
    function addsimpal() {
        //选择tree
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        debugger;
        if (selectedNode.isParent) {
            swal("选择错误", "此区域无法添加车辆!", "warning");
            return false;
        } else {

        }
        $.ajax({
            type: "GET",
            url: "/baseCar/create",
            cache: false,
            async: false,
            dataType: "json",
            success: function (data) {
                $('#editFormFast').loadDate(data);
                $("#myModalSimpal").modal("show");
            }
        });

    }
    function edit(index) {
        var data = table.DataTable().rows(index).data()
        if (data && data[0]) {
            loadComb("baseCarTypeId", combCarType);
            loadComb("baseTerminalId", combTerminal);
            $('#editForm').loadDate(data[0]);
            $("#myModal").modal("show");
        } else {
            swal("已取消", "数据错误！", "warning");
        }
    }
    function del(id) {
        swal({
                    title: "您确定要删除这条信息吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，我要删除！",
                    cancelButtonText: "让我再考虑一下…",
                    closeOnConfirm: false,
                    closeOnCancel: false
                },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            type: "GET",
                            url: "/baseCar/del",
                            cache: false,
                            async: false,
                            data: {
                                ids: id
                            },
                            dataType: "json",
                            success: function (data) {
//                            swal("删除成功！", "您已经永久删除了这条信息。", "success");
                                swal("删除成功！", data, "success");
                                query(0);
                            }
                        });

                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                });
    }
    function dels() {

        var rows = table.DataTable().rows('.selected').data();
        if (rows && rows.length >= 1) {

            var ids = "";
            rows.each(function (value) {
                ids += value.id + ",";
            })
            debugger;
            del(ids);
        } else {
            swal("系统提示", "请选择需要删除的数据！", "warning");
        }

    }
    function query(qtype) {
        var code;
        var jsonstr;

        if(qtype==1){
            var selectedNode = tree_Obj.getSelectedNodes()[0];
            var deptID;
            if (selectedNode) {
                if (selectedNode.isParent) {
                    return;
                } else {
                    deptID = selectedNode.id;
                }
                code = $("#serchStr").val();
                jsonstr = [{"deptid": deptID}, {"code": code}];
            }
        }else if(qtype==2){
            debugger;
            code = $("#serchStr").val();
            jsonstr = [{"basecarno": code},{"code": code}];
        }else if(qtype==3){
            debugger;
            code = $("#baseTerminalIdsearch").val();
            jsonstr = [{"baseterminalid": code},{"code": code}];
        }else{
            code = "";
            jsonstr = "";
        }


        table.DataTable().search(JSON.stringify(jsonstr), false, false).draw();
    }

    function savefast() {
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        $("#deptId").val(selectedNode.id);
        $('#editFormFast').ajaxSubmit({
            type: 'post', // 提交方式 get/post
            dataType: 'json',
            url: '/baseCar/save', // 需要提交的 url
            beforeSubmit: function (formData, jqForm, options) {
                if (!$("#editFormFast").valid()) {
                    return false;
                }
            },
            success: function (data) {
                // data 保存提交后返回的数据，一般为 json 数据
                // 此处可对 data 作相关处理
                if (data.success) {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "success"
                    });
                    $("#myModalSimpal").setDefForm(validatorfast);
                    $('#editFormFast').clearForm(true);
                    $("#myModalSimpal").modal("hide");
                    query(0);
                } else {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "error"
                    });
                }
            },

        });
    }




    function save() {
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        $("#deptId").val(selectedNode.id);
        $('#editForm').ajaxSubmit({
            type: 'post', // 提交方式 get/post
            dataType: 'json',
            url: '/baseCar/save', // 需要提交的 url
            beforeSubmit: function (formData, jqForm, options) {
                if (!$("#editForm").valid()) {
                    return false;
                }
            },
            success: function (data) {
                // data 保存提交后返回的数据，一般为 json 数据
                // 此处可对 data 作相关处理
                if (data.success) {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "success"
                    });
                    $("#myModal").setDefForm(validator);
                    $('#editForm').clearForm(true);
                    $("#myModal").modal("hide");
                    query(0);
                } else {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "error"
                    });
                }
            },

        });
    }
    function closeModal() {
        $("#myModal").setDefForm(validator);
        $("#myModal").modal("hide");
    }

    function closeModalSimpal() {
        $("#myModalSimpal").setDefForm(validatorfast);
        $("#myModalSimpal").modal("hide");
    }


    //隐藏和显示列 c:列0开始，visible=false隐藏
    function visiblecolumn(c, visible) {
        debugger;
        table.DataTable().column(c).visible(visible)
    }
    function loadComb(id, data) {
        $("#" + id).select2({
            data: data,
            theme: "bootstrap",
            placeholder: '请选择',
            allowClear: true
        });
    }

    //    部门车辆树
    $(function () {
        var url = '/tree/dept';
//        var listurl="<g:createLink base=".." controller="baseTerminal" action="list"  />";
        var listurl = "";
        init_tree('id', 'parentId', 'name', true, fnClick);
        loadMyTree(url);

    });

    //    树节点选中
    var fnClick = function (event, treeId, treeNode) {

        query(1);

    }


</script>


</body>

</html>
